package ${basepackage}.controller;

import javax.validation.Valid;

import com.excalibur.core.util.SpringUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import ${basepackage}.controller.basic.DateTimeBinderController;
import ${basepackage}.manager.${table.generateClassName}Manager;
import ${basepackage}.manager.exception.ServiceException;
import ${basepackage}.model.criteria.${databaseNameCamel}.${table.generateClassName}Criteria;
import ${basepackage}.model.json.JsonResponse;
import ${basepackage}.model.vo.common.Page;
import ${basepackage}.model.vo.${databaseNameCamel}.${table.unCapGenerateClassName}.Add${table.generateClassName}Form;
import ${basepackage}.model.vo.${databaseNameCamel}.${table.unCapGenerateClassName}.${table.generateClassName}Row;
import ${basepackage}.model.vo.${databaseNameCamel}.${table.unCapGenerateClassName}.Update${table.generateClassName}Form;


@Controller
@RequestMapping(value = "/${databaseNameCamel}/${table.unCapGenerateClassName}")
public class ${table.generateClassName}Controller extends DateTimeBinderController {

	private final static Logger logger = LoggerFactory
			.getLogger(${table.generateClassName}Controller.class);

	@Autowired
	private ${table.generateClassName}Manager ${table.unCapGenerateClassName}Manager;


	/**
	 * 后台管理页.
	 * 
	 * @return
	 */
	@RequestMapping(value = "")
	public String ${table.generateClassName}ListPage() {
		return "${databaseNameCamel}/${table.unCapGenerateClassName}";
	}
	
	/**
	 * 获取列表.
	 * 
	 * @param offset
	 * @param limit
	 * @return
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public JsonResponse get${table.generateClassName}List(int offset, int limit,
			${table.generateClassName}Criteria criteria) {
		JsonResponse jsonResponse = new JsonResponse();
		boolean result = false;
		Page<${table.generateClassName}Row> ${table.unCapGenerateClassName}List = null;
		try {
			${table.unCapGenerateClassName}List = ${table.unCapGenerateClassName}Manager.get${table.generateClassName}s(
					criteria, offset, limit);
			jsonResponse.setCode(0);
			jsonResponse.setData(${table.unCapGenerateClassName}List);
			result = true;
		} catch (ServiceException e) {
			logger.error("Failed to get ${table.unCapGenerateClassName} list！", e);
			jsonResponse.setMessage(e.getMessage());
		} catch (DataAccessException e) {
			logger.error("Failed to get ${table.unCapGenerateClassName} list！", e);
			jsonResponse.setMessage(SpringUtils
					.getI18nMessage("webapp.controller.sysError"));
		}

		if (!result) {
			jsonResponse.setCode(1);
			${table.unCapGenerateClassName}List = new Page<${table.generateClassName}Row>();
			${table.unCapGenerateClassName}List.setTotal(0L);
			${table.unCapGenerateClassName}List.setRows(null);
			jsonResponse.setData(${table.unCapGenerateClassName}List);
		}

		return jsonResponse;
	}

	/**
	 * 删除.
	 * 
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public JsonResponse delete${table.generateClassName}(${table.primaryKey.type} ${table.primaryKey.name?uncap_first}) {
		JsonResponse jsonResponse = new JsonResponse();
		boolean result = false;
		try {
			result = ${table.unCapGenerateClassName}Manager.delete(${table.primaryKey.name?uncap_first});
			if (result) {
				jsonResponse.setCode(0);
				jsonResponse.setData(${table.primaryKey.name?uncap_first});
			} else {
				jsonResponse.setMessage(SpringUtils
						.getI18nMessage("webapp.controller.sysError"));
			}
		} catch (ServiceException e) {
			logger.error("Failed to delete ${table.generateClassName}！", e);
			jsonResponse.setMessage(e.getMessage());
		} catch (DataAccessException e) {
			logger.error("Failed to delete ${table.generateClassName}！", e);
			jsonResponse.setMessage(SpringUtils
					.getI18nMessage("webapp.controller.sysError"));
		}

		if (!result) {
			jsonResponse.setCode(1);
		}
		return jsonResponse;
	}

	/**
	 * 修改.
	 * 
	 * @param ${table.unCapGenerateClassName}Row
	 * @return
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public JsonResponse update${table.generateClassName}(
			@Valid Update${table.generateClassName}Form update${table.generateClassName}Form,
			BindingResult validResult) {
		JsonResponse jsonResponse = new JsonResponse();
		boolean result = false;
		// 验证结果检查
		if (validResult.hasErrors()) {
			for (FieldError error : validResult.getFieldErrors()) {
				jsonResponse.setMessage(error.getDefaultMessage());
				break;
			}
		} else {
			try {
				${table.generateClassName}Row ${table.unCapGenerateClassName}Row = ${table.unCapGenerateClassName}Manager
						.update${table.generateClassName}(update${table.generateClassName}Form);
				jsonResponse.setCode(0);
				jsonResponse.setData(${table.unCapGenerateClassName}Row);
				result = true;
			} catch (ServiceException e) {
				logger.error("Failed to update ${table.generateClassName}！", e);
				jsonResponse.setMessage(e.getMessage());
			} catch (DataAccessException e) {
				logger.error("Failed to update ${table.generateClassName}！", e);
				jsonResponse.setMessage(SpringUtils
						.getI18nMessage("webapp.controller.sysError"));
			}
		}

		if (!result) {
			jsonResponse.setCode(1);
		}

		return jsonResponse;
	}

	/**
	 * 添加.
	 * 
	 * @param ${table.unCapGenerateClassName}Row
	 * @return
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public JsonResponse add${table.generateClassName}(
			@Valid Add${table.generateClassName}Form add${table.generateClassName}Form,
			BindingResult validResult) {
		JsonResponse jsonResponse = new JsonResponse();
		boolean result = false;
		// 验证结果检查
		if (validResult.hasErrors()) {
			for (FieldError error : validResult.getFieldErrors()) {
				jsonResponse.setMessage(error.getDefaultMessage());
				break;
			}
		} else {
			try {
				${table.generateClassName}Row ${table.unCapGenerateClassName}Row = ${table.unCapGenerateClassName}Manager
						.add${table.generateClassName}(add${table.generateClassName}Form);
				jsonResponse.setCode(0);
				jsonResponse.setData(${table.unCapGenerateClassName}Row);
				result = true;
			} catch (ServiceException e) {
				logger.error("Failed to add ${table.generateClassName}！", e);
				jsonResponse.setMessage(e.getMessage());
			} catch (DataAccessException e) {
				logger.error("Failed to add ${table.generateClassName}！", e);
				jsonResponse.setMessage(SpringUtils
						.getI18nMessage("webapp.controller.sysError"));
			}
		}

		if (!result) {
			jsonResponse.setCode(1);
		}

		return jsonResponse;
	}

}
